﻿module Sustainalytics.EsgRatings {

    import Company = Sustainalytics.Company;

    export class RelativePositionController {

        public esgRelativePosition: EsgRelativePositionDto;
        public esgRelativePosition_positionSummary_esgPositionName: string;

        private userId: string;
        private esgProfileId: string;

        constructor(
            private $scope: Company.ICompanyBasedScope,
            private $q: ng.IQService,
            private $timeout: ng.ITimeoutService,
            private $filter: ng.IFilterService,
            private EsgRelativePositionService: IEsgRelativePositionService,
            private NumberUtilityService: Common.NumberUtilityService,
            private $rootScope) {

            var ctx = this;

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.userId = ctx.$rootScope['userInfo'].userId;
            ctx.esgProfileId = ctx.$rootScope['userInfo'].esgProfileId;

            ctx.init();
        }

        private init(): ng.IPromise<any> {
            var ctx = this;

            var promise = ctx.getEsgRelativePosition();

            return promise;
        }

        private getEsgRelativePosition(): ng.IPromise<EsgRelativePositionDto> {
            var ctx = this;
            var promise = ctx.EsgRelativePositionService.get(ctx.$scope.currentCompany.id, ctx.esgProfileId);

            promise.then(function (res) {
                ctx.$timeout(function () {
                    ctx.$scope.$apply(function () {
                        ctx.esgRelativePosition = res;
                        ctx.esgRelativePosition_positionSummary_esgPositionName = ctx.esgRelativePosition.positionSummary.esgPosition.name;
                    });
                });
            },() => {

                    ga('create', 'UA-4379897-6', {
                        'userId': ctx.userId, 'cookieDomain': 'none', 'name': 'globalAccess'
                    });

                    ga('globalAccess.send', 'exception', { 'exDescription': 'Esg Ratings: Relative Position Controller -> Error loading Relative Position' });
                });

            return promise;
        }

        public getEsgPosition(): any {
            return EsgPositionEnum;
        }

    }


    export var relativePositionControllerName = 'RelativePositionController';

    angular.module(esgRatingsModuleName).controller(relativePositionControllerName,
        ['$scope', '$q', '$timeout', '$filter', 'EsgRelativePositionService', 'NumberUtilityService', '$rootScope',RelativePositionController]);
} 